<?php
class SiteAdmin_RegionController extends SiteAdminController
{
		
   	public function indexAction() {
    	$country = intval($this->getRequest()->getParam("country", 0));
    	$this->view->c = (int)$country;
    	$this->view->CountryPage = Obj_LocationContentPage::getLocationContentPages($country, CONTENT_TYPE_COUNTRY);
    	$ar = Obj_Country::getCountries();
    	$iCountry = $this->getRequest()->getParam('country', 0);
    	$countries = array();
    	$countries[0] = _('Выберите страну ...');
    	foreach ($ar as $v) {
    		$countries[$v['ID']] = $v['Name'];
    	} 
    	$this->view->countries = $countries;
    	$this->view->country = $iCountry;
    	$this->view->regions = Obj_Region::getRegions($iCountry);
    }

   	public function editAction() {
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	$addnew = intval($this->getRequest()->getParam('addnew', 0)) === 1 ? true : false;
    	$arRegion = Obj_Region::getRegion($id);
    	if ($addnew || $arRegion === false) {
    		$this->view->region = array(
    			'ID' => 0,
    			'CountryID' => 0,
    			'Name' => '',
    			'alias' => '',
    			'active' => 0,
    			'sort' => 0,
    			'seoTitle' => '',
    			'seoDescription' => '',
    			'seoKeywords' => '',
    		);
    	}
    	else {
    		$this->view->region = $arRegion;
    	}
    	$ar = Obj_Country::getCountries();
    	$countries = array();
    	foreach ($ar as $v) {
    		$countries[$v['ID']] = $v['Name'];
    	} 
    	$this->view->countries = $countries;
    }

    public function saveAction() {
    	if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$arRegion = array();
    	$id = (int) $this->getRequest()->getParam('ID', 0);
    	$arRegion['Name'] = trim($this->getRequest()->getParam('Name', ''));
    	if ($arRegion['Name'] == '') {
    		$this->_helper->json(array('error' => 1, 'errorStr' => _('Регион должен иметь название!')), true, false);
    	}
    	$arRegion['CountryID'] = intval($this->getRequest()->getParam('CountryID', ''));
    	if ($arRegion['CountryID'] === 0) {
    		$this->_helper->json(array('error' => 1, 'errorStr' => _('Регион должен иметь страну!')), true, false);
    	}
    	$arRegion['alias'] = Obj_Text::translit($arRegion['Name']);
    	$arRegion['sort'] = intval($this->getRequest()->getParam('sort', 0));
    	$arRegion['seoTitle'] = trim($this->getRequest()->getParam('seoTitle', ''));
    	$arRegion['seoDescription'] = trim($this->getRequest()->getParam('seoDescription', ''));
    	$arRegion['seoKeywords'] = trim($this->getRequest()->getParam('seoKeywords', ''));
    	$arRegion['active'] = intval($this->getRequest()->getParam('active', 0));
    	
    	$cid = Obj_Region::saveRegion($arRegion, $id);
    	if ($arRegion['active'] === 1) Obj_System_SearchIndex::write($cid, CONTENT_TYPE_REGION, $arRegion['Name']);
    	else Obj_System_SearchIndex::delete($cid, CONTENT_TYPE_REGION); 
    	
    	$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('controller' => 'region', 'action' => 'index', 'country'=>$arRegion['CountryID']))), true, false);
    }

    public function deleteAction() {
        if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	$arRegion = Obj_Region::getRegion($id);
    	Obj_Region::delRegion($id);
    	Obj_System_SearchIndex::delete($id, CONTENT_TYPE_REGION);
    	$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('controller' => 'region', 'action' => 'index', 'country'=>$arRegion['CountryID']))), true, false);
    }

    public function stateChangerAction() {
		if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	$arRegion = Obj_Region::getRegion($id);
    	if(intval($arRegion['active']) === 1){
    		$arRegion['active'] = 0;
    		$cid = Obj_Region::saveRegion($arRegion, $id);
    		Obj_System_SearchIndex::delete($id, CONTENT_TYPE_REGION);
    	} else {
    		$arRegion['active'] = 1;
    		$cid = Obj_Region::saveRegion($arRegion, $id);
    		Obj_System_SearchIndex::write($id, CONTENT_TYPE_REGION, $arRegion['Name']);
    	}
    	$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('controller' => 'region', 'action' => 'index', 'country'=>$arRegion['CountryID']))), true, false);
    }

    public function groupedDeleteAction(){
    	if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$arDelete = array();
    	$arDelete = $this->getRequest()->getParam('arDelete', null);
    	if(sizeOf($arDelete)>0){
    		$country = (int)Obj_Region::getRegion($arDelete[0]['ID']);
	    	foreach ($arDelete as $k => $v){
		    	$id = (int) $v;
		    	Obj_Region::delRegion($id);
		    	Obj_System_SearchIndex::delete($id, CONTENT_TYPE_REGION);
	    	}
			$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('controller' => 'region', 'action' => 'index', 'country'=>$country))), true, false);
		}
    }
}